import Vue from 'vue'
import VueRouter from 'vue-router'
import Vuex from '../store/index'
import Index from '../views/Index/Index'
import Detail from '../views/detail/Detail'
import Login from './moudel/login'
import Register from './moudel/register'
import pingjia from '../views/detail/pinjia'
import shangpin from '../views/detail/shangpin'
import xiangqing from '../views/detail/xiangqing'
Vue.use(VueRouter)

const routes = [
  {
    path: '/index',
    name: 'index',
    component: Index
  },
  {
    path: '/list',
    name: 'list',
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () => import(/* webpackChunkName: "about" */ '../views/list/List.vue')
  },
  {
    path: '/me',
    name: 'me',
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () => import(/* webpackChunkName: "me" */ '../views/me/Me.vue')
  },Login,Register,
  {
    path: '/search',
    name: 'search',
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () => import(/* webpackChunkName: "search" */ '../views/search/Search.vue')
  },  {
    path: '/searchlist',
    name: 'searchlist',
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () => import(/* webpackChunkName: "search" */ '../views/search/Searchlist.vue')
  },Login,Register,
  {
    path: '/search',
    name: 'search',
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () => import(/* webpackChunkName: "search" */ '../views/search/Search.vue')
  },
  {
    path: '/shopcar',
    name: 'shopcar',
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () => import(/* webpackChunkName: "shopcar" */ '../views/shopcar/ShopCar.vue'),
    meta:{
      title:'购物车'
    }
  },
  {
    path: '/shoplist',
    name: 'shopcarlist',
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () => import(/* webpackChunkName: "shopcar" */ '../views/shopcar/shopList.vue'),
    meta:{
      title:'购物列表'
    }
  },
  {
    path: '/gopay',
    name: 'gopay',
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () => import(/* webpackChunkName: "shopcar" */ '../views/shopcar/Gopay.vue'),
    meta:{
      title:'购物列表'
    }
  },
  {
    path: '/detail/:id',
    name: 'detail',
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: Detail,
    children:[
      {
        path:'xiangqing',
        name:'xiangqing',
        component:xiangqing
      },
      {
        path:'shangpin',
        name:'shangpin',
        component:shangpin

      },
      {
        path:'pingjia',
        name:'pingjia',

        component:pingjia

      },{
        path:'/detail/:id',
        redirect:'shangpin'
      }
    ]
  } ,
  {
    path: '/',
    name: 'Index',
    redirect:'index'
  },
]

const router = new VueRouter({
  mode: 'hash',
  base: process.env.BASE_URL,
  routes
})
router.beforeEach((to, from, next) => {
  // to and from are both route objects. must call `next`.
  let tak =sessionStorage.getItem("token")
  if(Vuex.state.token!='' || tak!=''){
      next()
  }else{
    if(to.path == "/login"){
        next()
    }else{
      next("/login")
    }
  }
})
export default router
